﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
using _1._6._2015_Entity.Data.Models;

namespace _1._6._2015_Entity.Data
{
    public static class Seeder
    {
        public static void Seed(OrdersDbContext db)
        {
            //build an object to look for this before you enter it into the db again and again to params on the addorupdate: where it is going and what it is check then add it
            db.Users.AddOrUpdate(u => new { u.FirstName, u.LastName },
                new User
                {
                    FirstName = "Jim",
                    LastName = "Misel"
                },
                new User
                {
                    FirstName = "Nick",
                    LastName = "Brittain"
                });
            db.Items.AddOrUpdate(i => i.Description,
                new Item
                {
                    Description = "Peanut Butter",
                    Price = 3.99m
                },
                   new Item
                {
                    Description = "Hot Dogs",
                    Price = 2.99m
                });

            //if no orders then they can be added
            if (!db.Orders.Any())
            {
                User Jim = db.Users.FirstOrDefault(u => u.FirstName == "Jim" && u.LastName == "Misel");

                //build the order here
                Order o = new Order
                {
                    OrderDate = DateTime.Now,
                    Total = 45.00m,
                    User = Jim
                };
                db.Orders.Add(o);

                OrderItem oi = new OrderItem
                {
                    Order = o,
                    Price = 65.45m,
                    Item = db.Items.FirstOrDefault()
                };
                db.OrderItems.Add(oi);
                OrderItem oi2 = new OrderItem
                {
                    Order = o,
                    Price = 12.99m,
                    Item = db.Items.FirstOrDefault(i => i.Description == "Hot Dogs")
                };
                db.OrderItems.Add(oi2);
                // entity will add this on its own so the saveCanges() is not really needed at the end its just so you know it is done
                db.SaveChanges();
            }
        }
    }
}
